/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.asset.inventory.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.asset.entity.AssetCard;
import com.fuyao.cloud.asset.inventory.dto.InventoryTaskAssetSearchDTO;
import com.fuyao.cloud.asset.inventory.entity.InventoryTask;
import com.fuyao.cloud.asset.inventory.enums.InventoryTaskStatusEnum;
import com.fuyao.cloud.asset.inventory.vo.InventoryTaskAssetVO;
import com.fuyao.cloud.common.biz.service.FyBaseServiceImpl;
import com.fuyao.cloud.asset.inventory.entity.InventoryTaskAsset;
import com.fuyao.cloud.asset.inventory.mapper.InventoryTaskAssetMapper;
import com.fuyao.cloud.asset.inventory.service.InventoryTaskAssetService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 盘点任务清单表
 *
 * @author lyhills
 * @date 2023-09-11 12:01:55
 */
@Slf4j
@Service
public class InventoryTaskAssetServiceImpl extends FyBaseServiceImpl<InventoryTaskAssetMapper, InventoryTaskAsset> implements InventoryTaskAssetService {

    @Override
    public InventoryTaskAsset create() {
        return new InventoryTaskAsset();
    }

    @Transactional
    @Override
    public boolean saveOrUpdateInventoryTaskAsset(InventoryTask entity, List<AssetCard> assetCardList) {
        if(CollUtil.isEmpty(assetCardList)){
            log.warn(String.format("InventoryTask id:%s get assetCard List isEmpty !!",entity.getId()));
            return false;
        }
        // 新建或变跟类型时重新生成清单数据
        this.remove(Wrappers.<InventoryTaskAsset>lambdaQuery().eq(InventoryTaskAsset::getInventoryTaskId, entity.getId()));
        List<InventoryTaskAsset> inventoryTaskAssetAddList = Lists.newArrayList();
        assetCardList.forEach(assetCard -> {
            InventoryTaskAsset taskAsset = new InventoryTaskAsset();
            taskAsset.setInventoryTaskId(entity.getId());
            taskAsset.setStatus(InventoryTaskStatusEnum.DRAFT.getCode());
            taskAsset.setAssetId(assetCard.getId());
            taskAsset.setDescription("");
            inventoryTaskAssetAddList.add(taskAsset);
        });
        return this.saveOrUpdateBatch(inventoryTaskAssetAddList);
    }

    @Override
    public IPage<InventoryTaskAssetVO> getInventoryTaskAssetPage(Page page, QueryWrapper<InventoryTaskAssetSearchDTO> query, InventoryTaskAssetSearchDTO searchDTO) {
        return this.baseMapper.getInventoryTaskAssetPage(page,query,searchDTO);
    }
}
